{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Peeking Iterator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #design #array #iterator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #设计 #数组 #迭代器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: PeekingIterator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #窥视迭代器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>请你在设计一个迭代器，在集成现有迭代器拥有的&nbsp;<code>hasNext</code> 和 <code>next</code> 操作的基础上，还额外支持 <code>peek</code> 操作。</p>\n",
    "\n",
    "<p>实现 <code>PeekingIterator</code> 类：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>PeekingIterator(Iterator&lt;int&gt; nums)</code> 使用指定整数迭代器&nbsp;<code>nums</code> 初始化迭代器。</li>\n",
    "\t<li><code>int next()</code> 返回数组中的下一个元素，并将指针移动到下个元素处。</li>\n",
    "\t<li><code>bool hasNext()</code> 如果数组中存在下一个元素，返回 <code>true</code> ；否则，返回 <code>false</code> 。</li>\n",
    "\t<li><code>int peek()</code> 返回数组中的下一个元素，但 <strong>不</strong> 移动指针。</li>\n",
    "</ul>\n",
    "\n",
    "<p><strong>注意：</strong>每种语言可能有不同的构造函数和迭代器&nbsp;<code>Iterator</code>，但均支持 <code>int next()</code> 和 <code>boolean hasNext()</code> 函数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>\n",
    "[\"PeekingIterator\", \"next\", \"peek\", \"next\", \"next\", \"hasNext\"]\n",
    "[[[1, 2, 3]], [], [], [], [], []]\n",
    "<strong>输出：</strong>\n",
    "[null, 1, 2, 2, 3, false]\n",
    "\n",
    "<strong>解释：</strong>\n",
    "PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [<u><strong>1</strong></u>,2,3]\n",
    "peekingIterator.next();    // 返回 1 ，指针移动到下一个元素 [1,<u><strong>2</strong></u>,3]\n",
    "peekingIterator.peek();    // 返回 2 ，指针未发生移动 [1,<u><strong>2</strong></u>,3]\n",
    "peekingIterator.next();    // 返回 2 ，指针移动到下一个元素 [1,2,<u><strong>3</strong></u>]\n",
    "peekingIterator.next();    // 返回 3 ，指针移动到下一个元素 [1,2,3]\n",
    "peekingIterator.hasNext(); // 返回 False\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>\n",
    "\t<li>对 <code>next</code> 和 <code>peek</code> 的调用均有效</li>\n",
    "\t<li><code>next</code>、<code>hasNext</code> 和 <code>peek </code>最多调用&nbsp; <code>1000</code> 次</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>进阶：</strong>你将如何拓展你的设计？使之变得通用化，从而适应所有的类型，而不只是整数型？</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [peeking-iterator](https://leetcode.cn/problems/peeking-iterator/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [peeking-iterator](https://leetcode.cn/problems/peeking-iterator/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"PeekingIterator\",\"next\",\"peek\",\"next\",\"next\",\"hasNext\"]\\n[[[1,2,3]],[],[],[],[],[]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.indx = -1\n",
    "        self.it = iterator\n",
    "        self.nums = [0 for i in range(1000)]\n",
    "        self.len = 0\n",
    "        while iterator.hasNext():\n",
    "\n",
    "            self.nums[self.len] = iterator.next()\n",
    "            #self.nums.append(iterator.next())\n",
    "            self.len += 1 \n",
    "\n",
    "    def peek(self):\n",
    "        return self.nums[self.indx + 1]\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        self.indx += 1\n",
    "        ret = self.nums[self.indx]\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.indx + 1 < self.len\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.peeked = deque()\n",
    "        self.iterator = iterator\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peeked:\n",
    "            return self.peeked[0]\n",
    "        self.peeked.append(self.iterator.next())\n",
    "        return self.peeked[0]\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peeked:\n",
    "            return self.peeked.popleft()\n",
    "        return self.iterator.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return len(self.peeked) > 0 or self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "\n",
    "    def peek(self):\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        return self._hasNext\n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.it = iterator\n",
    "        self.cur = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.cur == None:\n",
    "            self.cur = self.it.next()\n",
    "        return self.cur\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.cur != None:\n",
    "            temp = self.cur\n",
    "            self.cur = None\n",
    "            return temp\n",
    "        else:\n",
    "            return self.it.next()\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.it.hasNext() or (self.cur != None)\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.nums = []\n",
    "        while iterator.hasNext():\n",
    "            self.nums.append(iterator.next())\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.nums[0]\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.nums.pop(0)\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return len(self.nums) > 0\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.data = iterator\n",
    "        self.nx = iterator.next()\n",
    "        self.hasNx = True\n",
    "\n",
    "    def peek(self):\n",
    "        return self.nx\n",
    "        \n",
    "    def next(self):\n",
    "        mem = self.nx\n",
    "        self.hasNx = self.data.hasNext()\n",
    "        if self.hasNx:\n",
    "            self.nx = self.data.next()\n",
    "        return mem\n",
    "\n",
    "    def hasNext(self):\n",
    "        return self.hasNx\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\r\n",
    "#\r\n",
    "# class Iterator:\r\n",
    "#     def __init__(self, nums):\r\n",
    "#         \"\"\"\r\n",
    "#         Initializes an iterator object to the beginning of a list.\r\n",
    "#         :type nums: List[int]\r\n",
    "#         \"\"\"\r\n",
    "#\r\n",
    "#     def hasNext(self):\r\n",
    "#         \"\"\"\r\n",
    "#         Returns true if the iteration has more elements.\r\n",
    "#         :rtype: bool\r\n",
    "#         \"\"\"\r\n",
    "#\r\n",
    "#     def next(self):\r\n",
    "#         \"\"\"\r\n",
    "#         Returns the next element in the iteration.\r\n",
    "#         :rtype: int\r\n",
    "#         \"\"\"\r\n",
    "\r\n",
    "class PeekingIterator:\r\n",
    "    def __init__(self, iterator):\r\n",
    "        self.iter = iterator\r\n",
    "        self.buffer = []\r\n",
    "\r\n",
    "    def peek(self):\r\n",
    "        if self.buffer:\r\n",
    "            return self.buffer[0]\r\n",
    "        val = self.iter.next()\r\n",
    "        self.buffer.append(val)\r\n",
    "        return val\r\n",
    "\r\n",
    "    def next(self):\r\n",
    "        if self.buffer:\r\n",
    "            return self.buffer.pop(0)\r\n",
    "        else:\r\n",
    "            return self.iter.next()\r\n",
    "\r\n",
    "    def hasNext(self):\r\n",
    "        return bool(self.buffer) or self.iter.hasNext()\r\n",
    "        \r\n",
    "\r\n",
    "# Your PeekingIterator object will be instantiated and called as such:\r\n",
    "# iter = PeekingIterator(Iterator(nums))\r\n",
    "# while iter.hasNext():\r\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\r\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.it = iterator\n",
    "        self._cache = 0\n",
    "        self.has_cache = False\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not self.has_cache:\n",
    "            self.has_cache = True\n",
    "            self._cache = self.it.next()\n",
    "        return self._cache\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.has_cache:\n",
    "            self.has_cache = False\n",
    "            return self._cache\n",
    "        return self.it.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.has_cache or self.it.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\r\n",
    "#\r\n",
    "# class Iterator:\r\n",
    "#     def __init__(self, nums):\r\n",
    "#         \"\"\"\r\n",
    "#         Initializes an iterator object to the beginning of a list.\r\n",
    "#         :type nums: List[int]\r\n",
    "#         \"\"\"\r\n",
    "#\r\n",
    "#     def hasNext(self):\r\n",
    "#         \"\"\"\r\n",
    "#         Returns true if the iteration has more elements.\r\n",
    "#         :rtype: bool\r\n",
    "#         \"\"\"\r\n",
    "#\r\n",
    "#     def next(self):\r\n",
    "#         \"\"\"\r\n",
    "#         Returns the next element in the iteration.\r\n",
    "#         :rtype: int\r\n",
    "#         \"\"\"\r\n",
    "\r\n",
    "class PeekingIterator:\r\n",
    "    def __init__(self, iterator):\r\n",
    "        self.iter = iterator\r\n",
    "        self.val = 0\r\n",
    "\r\n",
    "    def peek(self):\r\n",
    "        if self.val:\r\n",
    "            return self.val\r\n",
    "        val = self.iter.next()\r\n",
    "        self.val = val\r\n",
    "        return val\r\n",
    "\r\n",
    "    def next(self):\r\n",
    "        if self.val:\r\n",
    "            val = self.val\r\n",
    "            self.val = 0\r\n",
    "            return val\r\n",
    "        else:\r\n",
    "            return self.iter.next()\r\n",
    "\r\n",
    "    def hasNext(self):\r\n",
    "        return bool(self.val) or self.iter.hasNext()\r\n",
    "        \r\n",
    "\r\n",
    "# Your PeekingIterator object will be instantiated and called as such:\r\n",
    "# iter = PeekingIterator(Iterator(nums))\r\n",
    "# while iter.hasNext():\r\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\r\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iter = iterator\n",
    "        self.nexts = self.iter.next()\n",
    "\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        return self.nexts\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        t = self.nexts\n",
    "        self.nexts = self.iter.next()\n",
    "        return t\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        return True if self.nexts != -100000 else False\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.it = iterator\n",
    "        self.top = None\n",
    "\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not self.top:\n",
    "            self.top = self.it.next()\n",
    "        return self.top\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        #如果self.top为空,应该返回的是当前it.next对应元素\n",
    "        if not self.top:\n",
    "            return self.it.next()\n",
    "        res = self.top\n",
    "        self.top = None\n",
    "        return res\n",
    "        \n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if self.top:\n",
    "            return True\n",
    "        return self.it.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._hasnext = iterator.hasNext()\n",
    "        self._next = iterator.next() \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "        res = self._next\n",
    "        if self.iterator.hasNext():\n",
    "            self._next = self.iterator.next()\n",
    "            self._hasnext =True\n",
    "        else:\n",
    "            self._hasnext  =False \n",
    "        return res\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasnext\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()       \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if  self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.peek_val = None\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\" \n",
    "        if self.peek_val is None:\n",
    "            self.peek_val = self.iterator.next()\n",
    "        return self.peek_val\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peek_val is None:\n",
    "            return self.iterator.next()\n",
    "        val = self.peek_val\n",
    "        self.peek_val = None\n",
    "        return val\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.peek_val is not None or self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.__nums = []\n",
    "        while iterator.hasNext():\n",
    "            self.__nums.append(iterator.next())\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.__nums[0]\n",
    "\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.__nums.pop(0)\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return len(self.__nums) != 0\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "\n",
    "    def peek(self):\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        return self._hasNext\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.nums = []\n",
    "        self.n = 0\n",
    "        while(iterator.hasNext()):\n",
    "            self.nums.append(iterator.next())\n",
    "            self.n += 1\n",
    "        self.idx = 0\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.nums[self.idx]\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        num = self.nums[self.idx]\n",
    "        self.idx += 1\n",
    "        return num\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.idx < self.n\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "\n",
    "\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "        \n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.peak = None #缓存的下一个数，None表示还没缓存当前位置的下一个数\n",
    "        self.iter = iterator\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peak is None: #若未缓存下一个元素则去缓存\n",
    "            self.peak = self.iter.next() #且移动了指针\n",
    "        return self.peak\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peak is not None: #如果已经缓存了下一个数，则直接返回这个值为结果，且将缓存的下一个数置为None\n",
    "            val = self.peak\n",
    "            self.peak = None\n",
    "            return val\n",
    "        return self.iter.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        # 如果缓存了下一个数，或者下一个数有值，则返回True\n",
    "        return self.peak is not None or self.iter.hasNext()\n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        \n",
    "        self.cache = None\n",
    "        self.iter = iterator\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.cache is None:\n",
    "            self.cache = self.iter.next()\n",
    "        return self.cache\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.cache:\n",
    "            tmp = self.cache \n",
    "            self.cache = None\n",
    "            return tmp\n",
    "        \n",
    "        return self.iter.next()\n",
    "\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if self.cache:\n",
    "            return True\n",
    "        return self.iter.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iter = iterator\n",
    "        self.nxt = None\n",
    "        if self.iter.hasNext():\n",
    "            self.nxt = self.iter.next()\n",
    "\n",
    "    def peek(self):\n",
    "        return self.nxt\n",
    "\n",
    "    def next(self):\n",
    "        res = self.nxt\n",
    "        if self.iter.hasNext():\n",
    "            self.nxt = self.iter.next()\n",
    "        else:\n",
    "            self.nxt = None\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        if self.nxt != None:\n",
    "            return True\n",
    "        return False\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "        \n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        \n",
    "        return ret\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.value = 0\n",
    "        self.next_value = self.iterator.next()\n",
    "        \n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.next_value\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        self.value = self.next_value\n",
    "        self.next_value = self.iterator.next() if self.iterator.hasNext() else None\n",
    "        return self.value\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.next_value is not None\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val].\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iter = iterator\n",
    "        self.pk = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.pk is None:\n",
    "            self.pk = self.iter.next()\n",
    "        return self.pk\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.pk is not None:\n",
    "            val = self.pk\n",
    "            self.pk = None\n",
    "            return val\n",
    "        return self.iter.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.pk is not None or self.iter.hasNext()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator \n",
    "        self.next_cache = None \n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.next_cache is None:\n",
    "            self.next_cache = self.next()\n",
    "        return self.next_cache\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.next_cache: \n",
    "            val = self.next_cache \n",
    "            self.next_cache = None \n",
    "            return val \n",
    "        else: \n",
    "            return self.iterator.next() \n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if self.next_cache: \n",
    "            return True \n",
    "        else: \n",
    "            return self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.peeked = False\n",
    "        self.peeked_value = 0\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not self.peeked:\n",
    "            self.peeked_value = self.iterator.next()\n",
    "            self.peeked = True\n",
    "        return self.peeked_value\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not self.peeked:\n",
    "            return self.iterator.next()\n",
    "        self.peeked = False\n",
    "        return self.peeked_value\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.iterator.hasNext() or self.peeked\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iterator=iterator\n",
    "        self.array=[]\n",
    "    def peek(self):\n",
    "        if self.array:\n",
    "            return self.array[-1]\n",
    "        else:#空\n",
    "            self.array.append(self.iterator.next())\n",
    "        return self.array[-1]\n",
    "    def next(self):\n",
    "        if self.array:\n",
    "            return self.array.pop()\n",
    "        else:\n",
    "            return self.iterator.next()\n",
    "    def hasNext(self):\n",
    "        if self.array:\n",
    "            return True\n",
    "        return self.iterator.hasNext()\n",
    "       \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iter = iterator\n",
    "        self.nxt = None\n",
    "        if self.iter.hasNext():\n",
    "            self.nxt = self.iter.next()\n",
    "        print(\"11\")\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        return self.nxt\n",
    "\n",
    "    def next(self):\n",
    "        res = self.nxt\n",
    "        if self.iter.hasNext():\n",
    "            self.nxt = self.iter.next()\n",
    "        else:\n",
    "            self.nxt = None\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        if self.nxt != None:\n",
    "            return True\n",
    "        return False\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator=iterator\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.iterator.v[self.iterator.index]\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.iterator.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        temp = copy.copy(self.iterator)\n",
    "        res = temp.next()\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.iterator.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        return self.iterator.hasNext()\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.peeked = None\n",
    "        self.iterator = iterator\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peeked:\n",
    "            return self.peeked\n",
    "        self.peeked = self.iterator.next()\n",
    "        return self.peeked\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peeked != None:\n",
    "            p = self.peeked\n",
    "            self.peeked = None\n",
    "            return p\n",
    "        return self.iterator.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.peeked != None or self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iterator = iterator\n",
    "        self.has_next = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next()       \n",
    "\n",
    "    def peek(self):\n",
    "        return self._next\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        result = self._next\n",
    "        self.has_next = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self.has_next else 0\n",
    "        return result\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        return self.has_next\n",
    "        \n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iter = iterator\n",
    "        self.pk = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.pk is None:\n",
    "            self.pk = self.iter.next()\n",
    "        return self.pk\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.pk is not None:\n",
    "            val = self.pk\n",
    "            self.pk = None\n",
    "            return val\n",
    "        return self.iter.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.pk is not None or self.iter.hasNext()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "        \n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.i = iterator\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        z=copy.deepcopy(self.i)\n",
    "        \n",
    "        return z.next()\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.i.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        \n",
    "        return self.i.hasNext()\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.next_val = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.next_val:\n",
    "            return self.next_val\n",
    "        else:\n",
    "            self.next_val = self.iterator.next()\n",
    "        return self.next_val\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.next_val:\n",
    "            out = self.next_val\n",
    "            self.next_val = None\n",
    "            return out\n",
    "        else:\n",
    "            return self.iterator.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.next_val is not None or self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iter = iterator\n",
    "        self.cur = iterator.next()\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.cur\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ans = self.cur\n",
    "        if self.iter.hasNext():\n",
    "            self.cur = self.iter.next()\n",
    "        else:\n",
    "            self.cur = 0\n",
    "        return ans\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.cur != 0\n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iter = iterator\n",
    "        self.nxt = None\n",
    "        if self.iter.hasNext():\n",
    "            self.nxt = self.iter.next()\n",
    "        print(\"11\")\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        return self.nxt\n",
    "\n",
    "    def next(self):\n",
    "        res = self.nxt\n",
    "        if self.iter.hasNext():\n",
    "            self.nxt = self.iter.next()\n",
    "        else:\n",
    "            self.nxt = None\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        if self.nxt != None:\n",
    "            return True\n",
    "        return False\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iter = iterator\n",
    "        self.peeked = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.iter.hasNext() and not self.peeked:\n",
    "            self.peeked = self.iter.next()\n",
    "        return self.peeked\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peeked:\n",
    "            tmp = self.peeked\n",
    "            self.peeked = None\n",
    "            return tmp\n",
    "        else:\n",
    "            return self.iter.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if self.peeked: return True\n",
    "        return self.iter.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.array = []\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.array:\n",
    "            return self.array[-1]\n",
    "        else:\n",
    "           self.array.append(self.iterator.next())#' next的（）不要忘了\n",
    "           return self.array[-1]\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.array:\n",
    "            return self.array.pop()\n",
    "        else:\n",
    "            return self.iterator.next()\n",
    "\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if self.array:\n",
    "            return True\n",
    "        else: return self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iter = iterator\n",
    "        self.next_item = self.iter.next()\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.next_item\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        res = self.next_item\n",
    "        if self.iter.hasNext():\n",
    "            self.next_item = self.iter.next()\n",
    "        else:\n",
    "            self.next_item = None\n",
    "\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.next_item is not None\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.real_hasNext = iterator.hasNext()\n",
    "        self.the_next = iterator.next()\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.the_next\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self.the_next\n",
    "        self.real_hasNext = self.iterator.hasNext()\n",
    "        if self.real_hasNext:\n",
    "            self.the_next = self.iterator.next()\n",
    "        else:\n",
    "            self.the_next = 0\n",
    "        return ret\n",
    "\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.real_hasNext\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.current = self.iterator.next()\n",
    "\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.current\n",
    "\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        temp = self.current\n",
    "        self.current = self.iterator.next() if self.iterator.hasNext() else None\n",
    "        return temp\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.current is not None\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._hasNext = iterator.hasNext()\n",
    "        self._next = iterator.next()\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iter = iterator\n",
    "        self.pk = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.pk is None:\n",
    "            self.pk = self.iter.next()\n",
    "        return self.pk\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.pk is not None:\n",
    "            val = self.pk\n",
    "            self.pk = None\n",
    "            return val\n",
    "        return self.iter.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.pk is not None or self.iter.hasNext()\n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iterator = iterator\n",
    "        self.has_next = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next()\n",
    "\n",
    "\n",
    "    def peek(self):\n",
    "        return self._next\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        result = self._next\n",
    "        self.has_next = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self.has_next else 0\n",
    "        return result\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        return self.has_next\n",
    "        \n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.current = None\n",
    "        self.has = False\n",
    "        if self.iterator.hasNext():\n",
    "            self.nxt = self.iterator.next()\n",
    "            self.has = True\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.nxt\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        self.has = False\n",
    "        self.current = self.nxt\n",
    "        if self.iterator.hasNext():\n",
    "            self.nxt = self.iterator.next()\n",
    "            self.has = True\n",
    "        return self.current\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.has\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.__it = iterator\n",
    "        self.peek_num = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peek_num is None:\n",
    "            self.peek_num = self.__it.next()\n",
    "        return self.peek_num\n",
    "\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peek_num is not None:\n",
    "            re = self.peek_num\n",
    "            self.peek_num = None\n",
    "            return re\n",
    "        return self.__it.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.peek_num is not None or self.__it.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator_list=[]\n",
    "        while iterator.hasNext():\n",
    "            self.iterator_list.append(iterator.next())\n",
    "        self.index=0\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.hasNext():return self.iterator_list[self.index]\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.hasNext():\n",
    "            output=self.iterator_list[self.index]\n",
    "            self.index+=1\n",
    "            return output\n",
    "        else:\n",
    "            return None\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.index<len(self.iterator_list)\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class Iterator:\n",
    "    def __init__(self, nums):\n",
    "        self.nums = nums\n",
    "        self.index = 0\n",
    "\n",
    "    def hasNext(self):\n",
    "        return self.index < len(self.nums)\n",
    "\n",
    "    def next(self):\n",
    "        if self.hasNext():\n",
    "            value = self.nums[self.index]\n",
    "            self.index += 1\n",
    "            return value\n",
    "        else:\n",
    "            raise StopIteration(\"No more elements in the iterator\")\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iterator = iterator\n",
    "        self.peeked_value = None\n",
    "\n",
    "    def peek(self):\n",
    "        if not self.peeked_value and self.iterator.hasNext():\n",
    "            self.peeked_value = self.iterator.next()\n",
    "        return self.peeked_value\n",
    "\n",
    "    def next(self):\n",
    "        if self.peeked_value:\n",
    "            value = self.peeked_value\n",
    "            self.peeked_value = None\n",
    "        else:\n",
    "            value = self.iterator.next()\n",
    "        return value\n",
    "\n",
    "    def hasNext(self):\n",
    "        return self.peeked_value is not None or self.iterator.hasNext()\n",
    "\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\r\n",
    "#\r\n",
    "# class Iterator:\r\n",
    "#     def __init__(self, nums):\r\n",
    "#         \"\"\"\r\n",
    "#         Initializes an iterator object to the beginning of a list.\r\n",
    "#         :type nums: List[int]\r\n",
    "#         \"\"\"\r\n",
    "#\r\n",
    "#     def hasNext(self):\r\n",
    "#         \"\"\"\r\n",
    "#         Returns true if the iteration has more elements.\r\n",
    "#         :rtype: bool\r\n",
    "#         \"\"\"\r\n",
    "#\r\n",
    "#     def next(self):\r\n",
    "#         \"\"\"\r\n",
    "#         Returns the next element in the iteration.\r\n",
    "#         :rtype: int\r\n",
    "#         \"\"\"\r\n",
    "\r\n",
    "class PeekingIterator:\r\n",
    "    def __init__(self, iterator):\r\n",
    "        \"\"\"\r\n",
    "        Initialize your data structure here.\r\n",
    "        :type iterator: Iterator\r\n",
    "        \"\"\"\r\n",
    "        self.iterator = iterator\r\n",
    "        self._next = iterator.next()\r\n",
    "        self._hasNext = iterator.hasNext()\r\n",
    "        \r\n",
    "\r\n",
    "    def peek(self):\r\n",
    "        \"\"\"\r\n",
    "        Returns the next element in the iteration without advancing the iterator.\r\n",
    "        :rtype: int\r\n",
    "        \"\"\"\r\n",
    "        return self._next\r\n",
    "\r\n",
    "        \r\n",
    "\r\n",
    "    def next(self):\r\n",
    "        \"\"\"\r\n",
    "        :rtype: int\r\n",
    "        \"\"\"\r\n",
    "        ret = self._next\r\n",
    "        self._hasNext = self.iterator.hasNext()\r\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\r\n",
    "        return ret\r\n",
    "        \r\n",
    "\r\n",
    "    def hasNext(self):\r\n",
    "        \"\"\"\r\n",
    "        :rtype: bool\r\n",
    "        \"\"\"\r\n",
    "        return self._hasNext\r\n",
    "\r\n",
    "# Your PeekingIterator object will be instantiated and called as such:\r\n",
    "# iter = PeekingIterator(Iterator(nums))\r\n",
    "# while iter.hasNext():\r\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\r\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iter = iterator\n",
    "        self.pk = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.pk is None:\n",
    "            self.pk = self.iter.next()\n",
    "        return self.pk\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.pk is not None:\n",
    "            val = self.pk\n",
    "            self.pk = None\n",
    "            return val\n",
    "        return self.iter.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.pk is not None or self.iter.hasNext()\n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iter = iterator\n",
    "        self.prev_peek = -1\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.prev_peek != -1:\n",
    "            return self.prev_peek\n",
    "        self.prev_peek = self.iter.next()\n",
    "        return self.prev_peek\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.prev_peek != -1:\n",
    "            res = self.prev_peek\n",
    "            self.prev_peek = -1\n",
    "            return res\n",
    "        else:\n",
    "            return self.iter.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.prev_peek != -1 or self.iter.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.next_element = None\n",
    "        if self.iterator.hasNext():\n",
    "            self.next_element = self.iterator.next()\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.next_element\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        result = self.next_element\n",
    "        if self.iterator.hasNext():\n",
    "            self.next_element = self.iterator.next()\n",
    "        else:\n",
    "            self.next_element = None\n",
    "        return result\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.next_element is not None or self.iterator.hasNext()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.it=iterator\n",
    "        self.p=None \n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not self.p:\n",
    "            self.p=self.it.next()\n",
    "        return self.p\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.p:\n",
    "         v=self.p\n",
    "         self.p=None\n",
    "         return v\n",
    "        return  self.it.next() \n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.it.hasNext() or self.p is not None \n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "\n",
    "    def peek(self):\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        return self._hasNext\n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.it = iterator\n",
    "        self.top = None\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not self.top:\n",
    "            self.top = self.it.next()\n",
    "            return self.top\n",
    "        return self.top\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not self.top:\n",
    "            return self.it.next()\n",
    "        res = self.top\n",
    "        self.top = None\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if self.top:\n",
    "            return True\n",
    "        return self.it.hasNext()\n",
    "\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val].\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.iterator = iterator\n",
    "\n",
    "    def peek(self):\n",
    "        newIteratorList = []\n",
    "        while self.iterator.hasNext():\n",
    "            newIteratorList.append(self.iterator.next())\n",
    "        self.iterator = Iterator(newIteratorList)\n",
    "        return newIteratorList[0]\n",
    "\n",
    "    def next(self):\n",
    "        return self.iterator.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        return self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator=iterator\n",
    "        self._next=iterator.next()\n",
    "        self._hasNext=iterator.hasNext()\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret=self._next\n",
    "        self._hasNext=self.iterator.hasNext()\n",
    "        self._next=self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._hasNext = iterator.hasNext()\n",
    "        self._next = iterator.next()\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "\n",
    "# 作者：LEER\n",
    "# 链接：https://leetcode.cn/problems/peeking-iterator/solutions/2350033/284-ding-duan-die-dai-qi-ti-jie-by-leer-k5mdd/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.peak = None #缓存的下一个数，None表示还没缓存当前位置的下一个数\n",
    "        self.iter = iterator\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peak is None: #若未缓存下一个元素则去缓存\n",
    "            self.peak = self.iter.next()\n",
    "        return self.peak\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.peak is not None: #如果已经缓存了下一个数，则直接返回这个值为结果，且将缓存的下一个数置为None\n",
    "            val = self.peak\n",
    "            self.peak = None\n",
    "            return val\n",
    "        return self.iter.next()\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        # 如果缓存了下一个数，或者下一个数有值，则返回True\n",
    "        return self.peak is not None or self.iter.hasNext()\n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._hasNext = iterator.hasNext()\n",
    "        self._next = iterator.next()\n",
    "\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.ans=[]\n",
    "        while iterator.hasNext():\n",
    "            val=iterator.next()\n",
    "            self.ans.append(val)\n",
    "        self.cur=-1\n",
    "        self.n=len(self.ans)\n",
    "\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        self.cur+=1\n",
    "        t=self.ans[self.cur]\n",
    "        self.cur-=1\n",
    "        return t\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        self.cur+=1\n",
    "        t=self.ans[self.cur]\n",
    "        return t\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.cur+1<self.n\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self.next_item = None\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if self.next_item is None:\n",
    "            self.next_item = self.iterator.next()\n",
    "        return self.next_item\n",
    "        \n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "\n",
    "        if self.next_item is not None:\n",
    "            result = self.next_item\n",
    "            self.next_item = None\n",
    "            return result\n",
    "        else:\n",
    "            return self.iterator.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if self.next_item is not None:\n",
    "            return True\n",
    "        else:\n",
    "            return self.iterator.hasNext()\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "# \n",
    "#           Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext = iterator.hasNext()\n",
    "        \n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ret = self._next\n",
    "        self._hasNext = self.iterator.hasNext()\n",
    "        self._next = self.iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self.arr = [iterator.next()]\n",
    "        self.index = 0\n",
    "        while(iterator.hasNext()):\n",
    "            self.arr.append(iterator.next())\n",
    "        \n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.arr[self.index]\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        tmp = self.arr[self.index]\n",
    "        self.index += 1\n",
    "        return tmp\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self.index < len(self.arr)\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        self.data=list()\n",
    "        while iterator.hasNext():\n",
    "            self.data.append(iterator.next())\n",
    "        self.index=0\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        return self.data[self.index]\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        self.index+=1\n",
    "        return self.data[self.index-1]\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        return self.index<len(self.data)\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "        self._iterator = iterator\n",
    "        self._next = iterator.next()\n",
    "        self._hasNext= self._iterator.hasNext()\n",
    "\n",
    "\n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self._next\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "\n",
    "\n",
    "        ret = self._next\n",
    "        self._hasNext = self._iterator.hasNext()\n",
    "        self._next = self._iterator.next() if self._hasNext else 0\n",
    "        return ret\n",
    "\n",
    "\n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return self._hasNext\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "\n",
    "        self.container = []\n",
    "        while iterator.hasNext():\n",
    "            self.container.append(iterator.next())\n",
    "        self.cur = 0\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return self.container[self.cur]\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        self.cur += 1\n",
    "        return self.container[self.cur - 1]\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return not self.cur == len(self.container)\n",
    "\n",
    "\n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Below is the interface for Iterator, which is already defined for you.\n",
    "#\n",
    "# class Iterator:\n",
    "#     def __init__(self, nums):\n",
    "#         \"\"\"\n",
    "#         Initializes an iterator object to the beginning of a list.\n",
    "#         :type nums: List[int]\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def hasNext(self):\n",
    "#         \"\"\"\n",
    "#         Returns true if the iteration has more elements.\n",
    "#         :rtype: bool\n",
    "#         \"\"\"\n",
    "#\n",
    "#     def next(self):\n",
    "#         \"\"\"\n",
    "#         Returns the next element in the iteration.\n",
    "#         :rtype: int\n",
    "#         \"\"\"\n",
    "\n",
    "# 主要涉及到设计\n",
    "# 保存一个数值\n",
    "class PeekingIterator:\n",
    "    def __init__(self, iterator):\n",
    "        \"\"\"\n",
    "        Initialize your data structure here.\n",
    "        :type iterator: Iterator\n",
    "        \"\"\"\n",
    "\n",
    "        self.iterator = iterator\n",
    "        self.store = -1\n",
    "        self.flag = False  # false表示没有储存数值\n",
    "        \n",
    "\n",
    "    def peek(self):\n",
    "        \"\"\"\n",
    "        Returns the next element in the iteration without advancing the iterator.\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "\n",
    "        if self.flag:\n",
    "            return self.store\n",
    "        else:\n",
    "            self.store = self.iterator.next()\n",
    "            self.flag = True\n",
    "            return self.store\n",
    "        \n",
    "\n",
    "    def next(self):\n",
    "        \"\"\"\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "\n",
    "        if self.flag:\n",
    "            self.flag = False\n",
    "            return self.store\n",
    "        else:\n",
    "            return self.iterator.next()\n",
    "        \n",
    "\n",
    "    def hasNext(self):\n",
    "        \"\"\"\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "\n",
    "        return self.iterator.hasNext() or self.flag\n",
    "        \n",
    "\n",
    "# Your PeekingIterator object will be instantiated and called as such:\n",
    "# iter = PeekingIterator(Iterator(nums))\n",
    "# while iter.hasNext():\n",
    "#     val = iter.peek()   # Get the next element but not advance the iterator.\n",
    "#     iter.next()         # Should return the same value as [val]."
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
